from Student import *
class StudentManager:
    def __init__(self):
        self.juniors = []

    def add_junior(self, junior):
        self.juniors.append(junior)
        print('添加成功')

    def remove_junior(self, name):
        num = 0
        for junior in self.juniors:
            if junior.name == name:
                self.juniors.remove(junior)
                print('删除成功')
                num = 1
                break
        if num == 0:
            print('删除失败，没有该同学')

    def get_junior_info(self, name):


            for junior in self.juniors:
                if junior.name == name:
                     return junior
            raise NameError('没有查找到')



    def average_score(self):
        total = 0
        for junior in self.juniors:
            total += junior.score
        return total / len(self.juniors)


if __name__ =='__main__':
    ctrl = True
    junior_manage = StudentManager()
    while ctrl:
        print('*************')
        print("1.添加学员\n2.删除学员\n3.查询学员\n4.计算学员平均分\n5.退出")
        print('*************')
        try:
           num = int(input('请选择你的操作'))
        except ValueError:
            print('你输入的类型不正确，请从新输入')
            continue

        if num == 1:
            mark = True
            while mark:
                name = input('请输入姓名')
                score = int(input('请输入成绩'))
                stu = Junior(name, score)
                junior_manage.add_junior(stu)
                judge = input('是否继续（y/n）:')
                if judge == 'y':
                    mark = True
                else:
                    mark = False
        elif num == 2:
            mark = True
            while mark:
                name = input('请输入你要删除的学生的姓名')
                junior_manage.remove_junior(name)
                judge = input('是否继续（y/n）:')
                if judge == 'y':
                    mark = True
                else:
                    mark = False
        elif num == 3:
            mark = True
            while mark:
                name = input('请输入你要查询的学生的姓名')

                print(junior_manage.get_junior_info(name))
                judge = input('是否继续（y/n）:')
                if judge == 'y':
                    mark = True
                else:
                    mark = False
        elif num == 4:
            print(junior_manage.average_score())
        else:
            ctrl = False
            print('你退出学生管理系统')